package com.ruoyi.member.controller;

import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.inform.Inform;
import com.ruoyi.system.service.IInformService;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.domain.Member;
import com.ruoyi.member.service.IMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 会员信息Controller
 * 
 * @author ruoyi
 * @date 2023-04-07
 */
@RestController
@RequestMapping("/member/member")
public class MemberController extends BaseController
{
    @Autowired
    private IMemberService memberService;

    @Autowired
    private IInformService iInformService;

    /**
     * 王雅诺需要的代码
     */
    @PreAuthorize("@ss.hasPermi('member:member:getPerson')")
    @GetMapping("/getPerson")
    public List<Member> getPerson(Integer person_id){
        System.out.println(person_id+"我在这儿");
        return memberService.getPerson(person_id);
    }

    @PreAuthorize("@ss.hasPermi('member:member:getEmailByName')")
    @GetMapping("/getEmailByName")
    public Member getEmailByName(String memberName){
        System.out.println("看看我的名字是 :"+memberName);
        return memberService.getEmailByName(memberName);
    }


    /**
     * 查询会员信息列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member member)
    {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出会员信息列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:export')")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Member member)
    {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        util.exportExcel(response, list, "会员信息数据");
    }

    /**
     * 文件导入
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        List<Member> memberList = util.importExcel(file.getInputStream());
        for (int i = 0; i <memberList.size(); i++) {
            Member member = memberList.get(i);
            memberService.insertMember(member);
        }
        return AjaxResult.success();
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        return util.importTemplateExcel("会员数据");
    }



    /**
     * 获取会员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Integer memberId)
    {
        return success(memberService.selectMemberByMemberId(memberId));
    }

    /**
     * 新增会员信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:add')")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member)
    {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改会员信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member member)
    {
        Inform inform = new Inform();
        inform.setM_id(member.getMemberId());
        inform.setNotice(member.getPayState());
        inform.setState(member.getState());

        iInformService.updateInformTwo(inform);

        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除会员信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:remove')")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Integer[] memberIds)
    {
        return toAjax(memberService.deleteMemberByMemberIds(memberIds));
    }



}
